<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>vue-cli-service | Tab</title><meta name="keywords" content="CLI"><meta name="author" content="Tab"><meta name="copyright" content="Tab"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="vue-cli-service 机制npm run dev 之后发生了些什么？？？ 入口从package.json里面可以看到npm run dev其实就是vue-cli-service serve vue-cli3.0 安装的时候把vue-cli-service一并安装了，即执行了npm install vue-cli-service --save-dev 这样就可以在.&#x2F;node_module">
<meta property="og:type" content="article">
<meta property="og:title" content="vue-cli-service">
<meta property="og:url" content="https://tabxx.github.io/tab.github.io/2019/09/05/vue-cli-service/index.html">
<meta property="og:site_name" content="Tab">
<meta property="og:description" content="vue-cli-service 机制npm run dev 之后发生了些什么？？？ 入口从package.json里面可以看到npm run dev其实就是vue-cli-service serve vue-cli3.0 安装的时候把vue-cli-service一并安装了，即执行了npm install vue-cli-service --save-dev 这样就可以在.&#x2F;node_module">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://tabxx.github.io/tab.github.io/2019/09/05/vue-cli-service/topbg.jpg">
<meta property="article:published_time" content="2019-09-05T01:46:34.000Z">
<meta property="article:modified_time" content="2021-08-11T11:37:52.077Z">
<meta property="article:author" content="Tab">
<meta property="article:tag" content="CLI">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://tabxx.github.io/tab.github.io/2019/09/05/vue-cli-service/topbg.jpg"><link rel="shortcut icon" href="/tab.github.io/images/favicon.ico"><link rel="canonical" href="https://tabxx.github.io/tab.github.io/2019/09/05/vue-cli-service/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//hm.baidu.com"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/tab.github.io/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?050bcda3cb147a8c0de1c17a337c5466";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script><script>const GLOBAL_CONFIG = { 
  root: '/tab.github.io/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: true,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'vue-cli-service',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-08-11 19:37:52'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
          const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
          const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
          const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified

          if (t === undefined) {
            if (isLightMode) activateLightMode()
            else if (isDarkMode) activateDarkMode()
            else if (isNotSpecified || hasNoSupport) {
              const now = new Date()
              const hour = now.getHours()
              const isNight = hour <= 6 || hour >= 18
              isNight ? activateDarkMode() : activateLightMode()
            }
            window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
              if (saveToLocal.get('theme') === undefined) {
                e.matches ? activateDarkMode() : activateLightMode()
              }
            })
          } else if (t === 'light') activateLightMode()
          else activateDarkMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    })(window)</script><meta name="generator" content="Hexo 5.1.1"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src= "" data-lazy-src="/tab.github.io/images/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/tab.github.io/archives/"><div class="headline">文章</div><div class="length-num">10</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tab.github.io/tags/"><div class="headline">标签</div><div class="length-num">7</div></a></div></div></div><hr/></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/tab.github.io/2019/09/05/vue-cli-service/topbg.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/tab.github.io/">Tab</a></span><div id="menus"><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">vue-cli-service</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2019-09-05T01:46:34.000Z" title="发表于 2019-09-05 09:46:34">2019-09-05</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-08-11T11:37:52.077Z" title="更新于 2021-08-11 19:37:52">2021-08-11</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.7k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>8分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="vue-cli-service"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h3 id="vue-cli-service-机制"><a href="#vue-cli-service-机制" class="headerlink" title="vue-cli-service 机制"></a>vue-cli-service 机制</h3><p><code>npm run dev</code> 之后发生了些什么？？？</p>
<h4 id="入口"><a href="#入口" class="headerlink" title="入口"></a>入口</h4><p>从<code>package.json</code>里面可以看到<code>npm run dev</code>其实就是<code>vue-cli-service serve</code></p>
<p>vue-cli3.0 安装的时候把<code>vue-cli-service</code>一并安装了，即执行了<code>npm install vue-cli-service --save-dev</code></p>
<p>这样就可以在<code>./node_modules/.bin</code>目录下查看到<code>vue-cli-service</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">@IF EXIST <span class="string">&quot;%~dp0\node.exe&quot;</span> (</span><br><span class="line">  <span class="string">&quot;%~dp0\node.exe&quot;</span>  <span class="string">&quot;%~dp0\..\@vue\cli-service\bin\vue-cli-service.js&quot;</span> %*</span><br><span class="line">) ELSE (</span><br><span class="line">  @SETLOCAL</span><br><span class="line">  @SET PATHEXT=%PATHEXT:;.JS;=;%</span><br><span class="line">  node  <span class="string">&quot;%~dp0\..\@vue\cli-service\bin\vue-cli-service.js&quot;</span> %*</span><br><span class="line">)</span><br></pre></td></tr></table></figure>

<blockquote>
<p>入口：./node_modules/@vue/cli-service/bin/vue-cli-service.js</p>
</blockquote>
<p>瞅一眼vue-cli-service.js的核心代码</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> Service = <span class="built_in">require</span>(<span class="string">&#x27;../lib/Service&#x27;</span>)</span><br><span class="line"><span class="comment">// 实例化Service</span></span><br><span class="line"><span class="comment">// VUE_CLI_CONTEXT为undefined，所以传入的值为process.cwd()及项目所在目录</span></span><br><span class="line"><span class="keyword">const</span> service = <span class="keyword">new</span> Service(process.env.VUE_CLI_CONTEXT || process.cwd())</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> rawArgv = process.argv.slice(<span class="number">2</span>)</span><br><span class="line"><span class="comment">// minimist解析工具来对命令行参数进行解析</span></span><br><span class="line"><span class="keyword">const</span> args = <span class="built_in">require</span>(<span class="string">&#x27;minimist&#x27;</span>)(rawArgv, &#123;</span><br><span class="line">  boolean: [</span><br><span class="line">    <span class="comment">// build</span></span><br><span class="line">    <span class="string">&#x27;modern&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;report&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;report-json&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;watch&#x27;</span>,</span><br><span class="line">    <span class="comment">// serve</span></span><br><span class="line">    <span class="string">&#x27;open&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;copy&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;https&#x27;</span>,</span><br><span class="line">    <span class="comment">// inspect</span></span><br><span class="line">    <span class="string">&#x27;verbose&#x27;</span></span><br><span class="line">  ]</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// &#123; </span></span><br><span class="line"><span class="comment">// 	_: [ &#x27;serve&#x27; ],</span></span><br><span class="line"><span class="comment">// 	modern: false,</span></span><br><span class="line"><span class="comment">// 	report: false,</span></span><br><span class="line"><span class="comment">// 	&#x27;report-json&#x27;: false,</span></span><br><span class="line"><span class="comment">// 	watch: false,</span></span><br><span class="line"><span class="comment">// 	open: false,</span></span><br><span class="line"><span class="comment">// 	copy: false,</span></span><br><span class="line"><span class="comment">// 	https: false,</span></span><br><span class="line"><span class="comment">// 	verbose: false ,</span></span><br><span class="line"><span class="comment">// &#125; </span></span><br><span class="line"><span class="keyword">const</span> command = args._[<span class="number">0</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment">// 执行service方法传入:&#x27;serve&#x27;、agrs、[&#x27;serve&#x27;,&#x27;--open&#x27;,...]</span></span><br><span class="line">service.run(command, args, rawArgv).catch(<span class="function"><span class="params">err</span> =&gt;</span> &#123;</span><br><span class="line">  error(err)</span><br><span class="line">  process.exit(<span class="number">1</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>



<h4 id="初始化配置"><a href="#初始化配置" class="headerlink" title="初始化配置"></a>初始化配置</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">module</span>.exports = <span class="class"><span class="keyword">class</span> <span class="title">Service</span> </span>&#123;</span><br><span class="line">  <span class="keyword">constructor</span> (context, &#123; plugins, pkg, inlineOptions, useBuiltIn &#125; = &#123;&#125;) &#123;</span><br><span class="line">    process.VUE_CLI_SERVICE = <span class="built_in">this</span></span><br><span class="line">    <span class="built_in">this</span>.initialized = <span class="literal">false</span></span><br><span class="line">    <span class="built_in">this</span>.context = context</span><br><span class="line">    <span class="built_in">this</span>.inlineOptions = inlineOptions</span><br><span class="line">    <span class="built_in">this</span>.webpackChainFns = []</span><br><span class="line">    <span class="built_in">this</span>.webpackRawConfigFns = []</span><br><span class="line">    <span class="built_in">this</span>.devServerConfigFns = []</span><br><span class="line">    <span class="built_in">this</span>.commands = &#123;&#125;</span><br><span class="line">    <span class="built_in">this</span>.pkgContext = context</span><br><span class="line">    <span class="comment">// 获取package.json中的依赖</span></span><br><span class="line">    <span class="built_in">this</span>.pkg = <span class="built_in">this</span>.resolvePkg(pkg)</span><br><span class="line">	</span><br><span class="line">	<span class="comment">// 如果有内联插件，不使用package.json中找到的插件</span></span><br><span class="line">	<span class="comment">// 最终得到的plugins为内置插件+@vue/cli-plugin-*</span></span><br><span class="line">	<span class="comment">// &#123;id: &#x27;xxx&#x27;,apply: require(&#x27;xxx&#x27;)&#125;</span></span><br><span class="line">    <span class="built_in">this</span>.plugins = <span class="built_in">this</span>.resolvePlugins(plugins, useBuiltIn)</span><br><span class="line"></span><br><span class="line">	<span class="comment">// 解析每个命令使用的默认模式</span></span><br><span class="line">	<span class="comment">//&#123; serve: &#x27;development&#x27;,</span></span><br><span class="line">	<span class="comment">// build: &#x27;production&#x27;,</span></span><br><span class="line">	<span class="comment">// inspect: &#x27;development&#x27; &#125;</span></span><br><span class="line">    <span class="built_in">this</span>.modes = <span class="built_in">this</span>.plugins.reduce(<span class="function">(<span class="params">modes, &#123; apply: &#123; defaultModes &#125;&#125;</span>) =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="built_in">Object</span>.assign(modes, defaultModes)</span><br><span class="line">    &#125;, &#123;&#125;)</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  <span class="comment">// ......</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol>
<li><p>获取package.json中的依赖。通过<code>read-pkg</code>这个包读取package.json文件，并以JSON格式返回赋值给this.pkg。</p>
</li>
<li><p>初始化相关插件。相关插件包括：内联插件、package.json中的cli-plugin-*插件。内联插件包含serve、build、inspect等。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">resolvePlugins (inlinePlugins, useBuiltIn) &#123;</span><br><span class="line">    <span class="keyword">const</span> idToPlugin = <span class="function"><span class="params">id</span> =&gt;</span> (&#123;</span><br><span class="line">      id: id.replace(<span class="regexp">/^.\//</span>, <span class="string">&#x27;built-in:&#x27;</span>),</span><br><span class="line">      apply: <span class="built_in">require</span>(id)</span><br><span class="line">    &#125;)</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">let</span> plugins</span><br><span class="line"></span><br><span class="line">    <span class="keyword">const</span> builtInPlugins = [</span><br><span class="line">      <span class="string">&#x27;./commands/serve&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;./commands/build&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;./commands/inspect&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;./commands/help&#x27;</span>,</span><br><span class="line">      <span class="comment">// config plugins are order sensitive</span></span><br><span class="line">      <span class="string">&#x27;./config/base&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;./config/css&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;./config/dev&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;./config/prod&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;./config/app&#x27;</span></span><br><span class="line">    ].map(idToPlugin)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (inlinePlugins) &#123;</span><br><span class="line">      <span class="comment">// ...</span></span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">		<span class="comment">// 开发环境依赖+生产环境的依赖中，获取cli-plugin-*插件</span></span><br><span class="line">      <span class="keyword">const</span> projectPlugins = <span class="built_in">Object</span>.keys(<span class="built_in">this</span>.pkg.devDependencies || &#123;&#125;)</span><br><span class="line">        .concat(<span class="built_in">Object</span>.keys(<span class="built_in">this</span>.pkg.dependencies || &#123;&#125;))</span><br><span class="line">        .filter(isPlugin)	<span class="comment">// 验证是否为vue-cli插件 cli-plugin-*</span></span><br><span class="line">        .map(idToPlugin)</span><br><span class="line">      plugins = builtInPlugins.concat(projectPlugins)</span><br><span class="line">    &#125;</span><br><span class="line">  </span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>初始化模式。解析每个命令使用的默认模式，例如serve对应的development，build对应production。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 解析每个命令使用的默认模式</span></span><br><span class="line"><span class="comment">//&#123; serve: &#x27;development&#x27;,</span></span><br><span class="line"><span class="comment">// build: &#x27;production&#x27;,</span></span><br><span class="line"><span class="comment">// inspect: &#x27;development&#x27; &#125;</span></span><br><span class="line"><span class="built_in">this</span>.modes = <span class="built_in">this</span>.plugins.reduce(<span class="function">(<span class="params">modes, &#123; apply: &#123; defaultModes &#125;&#125;</span>) =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="built_in">Object</span>.assign(modes, defaultModes)</span><br><span class="line">&#125;, &#123;&#125;)</span><br></pre></td></tr></table></figure>

<p>内联插件默认导出了一个defaultModes</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// serve.js</span></span><br><span class="line"><span class="built_in">module</span>.exports.defaultModes = &#123;</span><br><span class="line">  serve: <span class="string">&#x27;development&#x27;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>为什么要初始化模式？</p>
<p>​</p>
</li>
</ol>
<h4 id="解析命令行参数"><a href="#解析命令行参数" class="headerlink" title="解析命令行参数"></a>解析命令行参数</h4><p>minimist解析工具来对命令行参数进行解析。shell命令携带的参数解析出来。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> args = <span class="built_in">require</span>(<span class="string">&#x27;minimist&#x27;</span>)(rawArgv, &#123;</span><br><span class="line">  boolean: [</span><br><span class="line">    <span class="comment">// build</span></span><br><span class="line">    <span class="comment">// serve</span></span><br><span class="line">    <span class="string">&#x27;open&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;copy&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;https&#x27;</span>,</span><br><span class="line">  ]</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// &#x27;serve&#x27;、agrs、[&#x27;serve&#x27;,&#x27;--open&#x27;,...]</span></span><br></pre></td></tr></table></figure>



<h4 id="加载插件"><a href="#加载插件" class="headerlink" title="加载插件"></a>加载插件</h4><p>运行service.run方法，加载环境变量，加载用户配置，应用插件。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">async</span> run (name, args = &#123;&#125;, rawArgv = []) &#123;</span><br><span class="line">    <span class="keyword">const</span> mode = args.mode || (name === <span class="string">&#x27;build&#x27;</span> &amp;&amp; args.watch ? <span class="string">&#x27;development&#x27;</span> : 		  <span class="built_in">this</span>.modes[name])</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 加载环境变量，加载用户配置，应用插件</span></span><br><span class="line">    <span class="built_in">this</span>.init(mode)</span><br><span class="line">  </span><br><span class="line">    args._ = args._ || []</span><br><span class="line">  	<span class="comment">// 注册完的命令集里获取对应的命令</span></span><br><span class="line">    <span class="keyword">let</span> command = <span class="built_in">this</span>.commands[name]</span><br><span class="line">    <span class="comment">// ....</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">const</span> &#123; fn &#125; = command</span><br><span class="line">    <span class="keyword">return</span> fn(args, rawArgv)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol>
<li>加载环境变量，从项目根目录下的.env.(mode)文件读取环境变量，并写入到process.env（声明环境变量必须是VUE_APP_*，不然会被过滤掉）</li>
</ol>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">init (mode = process.env.VUE_CLI_MODE) &#123;</span><br><span class="line">	<span class="comment">// 加载development.env环境变量</span></span><br><span class="line">    <span class="keyword">if</span> (mode) &#123;</span><br><span class="line">      <span class="built_in">this</span>.loadEnv(mode)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">this</span>.loadEnv()</span><br><span class="line">	<span class="comment">// ...</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 加载环境变量方法（部分）</span></span><br><span class="line">loadEnv (mode) &#123;</span><br><span class="line">	<span class="comment">// 项目路径/.env.development</span></span><br><span class="line">    <span class="keyword">const</span> basePath = path.resolve(<span class="built_in">this</span>.context, <span class="string">`.env<span class="subst">$&#123;mode ? <span class="string">`.<span class="subst">$&#123;mode&#125;</span>`</span> : <span class="string">``</span>&#125;</span>`</span>)</span><br><span class="line">	<span class="comment">// 项目路径/.env.development.local</span></span><br><span class="line">    <span class="keyword">const</span> localPath = <span class="string">`<span class="subst">$&#123;basePath&#125;</span>.local`</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">const</span> load = <span class="function"><span class="params">path</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">const</span> env = dotenv.config(&#123; path, <span class="attr">debug</span>: process.env.DEBUG &#125;)</span><br><span class="line">        <span class="comment">// 这里会先调用 dotenv（位于 node_modules/dotenv ）的 config 函数，最终会返回这样的格式 &#123; parsed: &#123; YOUR_ENV_KEY: &#x27;你设定的环境变量值&#x27; &#125; &#125;，并且写入到process.env里面</span></span><br><span class="line">        dotenvExpand(env)</span><br><span class="line">        logger(path, env)</span><br><span class="line">    &#125;</span><br><span class="line">    load(localPath)</span><br><span class="line">    load(basePath)</span><br><span class="line">  </span><br><span class="line">    <span class="keyword">if</span> (mode) &#123;</span><br><span class="line">      <span class="keyword">const</span> defaultNodeEnv = (mode === <span class="string">&#x27;production&#x27;</span> || mode === <span class="string">&#x27;test&#x27;</span>)</span><br><span class="line">        ? mode</span><br><span class="line">        : <span class="string">&#x27;development&#x27;</span></span><br><span class="line">      <span class="keyword">if</span> (shouldForceDefaultEnv || process.env.NODE_ENV == <span class="literal">null</span>) &#123;</span><br><span class="line">        process.env.NODE_ENV = defaultNodeEnv</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>



<ol start="2">
<li>加载用户配置。读取vue.config.js文件内部的配置</li>
</ol>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">init (mode = process.env.VUE_CLI_MODE) &#123;</span><br><span class="line">    <span class="comment">// 加载用户配置</span></span><br><span class="line">    <span class="keyword">const</span> userOptions = <span class="built_in">this</span>.loadUserOptions()</span><br><span class="line">    <span class="built_in">this</span>.projectOptions = defaultsDeep(userOptions, defaults())</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<ol start="3">
<li>应用插件。前面初始化插件的时候(resolvePlugins方法)有一个apply属性，调用这个方法就可以导入对应的插件，这里就是调用了serve.js的方法。</li>
</ol>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//const idToPlugin = id =&gt; (&#123;</span></span><br><span class="line"><span class="comment">//    id: id.replace(/^.\//, &#x27;built-in:&#x27;),</span></span><br><span class="line"><span class="comment">//    apply: require(id)</span></span><br><span class="line"><span class="comment">//&#125;)</span></span><br><span class="line">init (mode = process.env.VUE_CLI_MODE) &#123;</span><br><span class="line">    <span class="comment">// 应用插件</span></span><br><span class="line">    <span class="built_in">this</span>.plugins.forEach(<span class="function">(<span class="params">&#123; id, apply &#125;</span>) =&gt;</span> &#123;</span><br><span class="line">      apply(<span class="keyword">new</span> PluginAPI(id, <span class="built_in">this</span>), <span class="built_in">this</span>.projectOptions)</span><br><span class="line">    &#125;)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 从项目配置文件中应用webpack配置</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">this</span>.projectOptions.chainWebpack) &#123;</span><br><span class="line">      <span class="built_in">this</span>.webpackChainFns.push(<span class="built_in">this</span>.projectOptions.chainWebpack)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">this</span>.projectOptions.configureWebpack) &#123;</span><br><span class="line">      <span class="built_in">this</span>.webpackRawConfigFns.push(<span class="built_in">this</span>.projectOptions.configureWebpack)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="4">
<li>注册命令。在service注册serve命令（前面run方法最后调用了fn函数，也就是registerCommand的第三个参数）</li>
</ol>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// serve.js</span></span><br><span class="line">api.registerCommand(<span class="string">&#x27;serve&#x27;</span>, &#123;</span><br><span class="line">    description: <span class="string">&#x27;start development server&#x27;</span>,</span><br><span class="line">    usage: <span class="string">&#x27;vue-cli-service serve [options] [entry]&#x27;</span>,</span><br><span class="line">    options: &#123;&#125;</span><br><span class="line">  &#125;, <span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">serve</span> (<span class="params">args</span>) </span>&#123;</span><br><span class="line">   <span class="comment">// ...</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>



<h4 id="配置、启动服务"><a href="#配置、启动服务" class="headerlink" title="配置、启动服务"></a>配置、启动服务</h4><p>最终执行的serve.js 内注册serve时传递的方法。webpack获取到配置之后，实例化<code>Compiler</code> 传递给<code>webpackDevServer</code>，通过<code>webpackDevServer</code>实现自动编译和热更新。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// serve.js serve函数</span></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">serve</span> (<span class="params">args</span>) </span>&#123;</span><br><span class="line">  <span class="comment">//创建webpack编译器</span></span><br><span class="line">  <span class="keyword">const</span> compiler = webpack(webpackConfig)</span><br><span class="line">  <span class="comment">// compiler.run()即可完成一次编译打包</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// 创建本地服务</span></span><br><span class="line">  <span class="keyword">const</span> server = <span class="keyword">new</span> WebpackDevServer(compiler, <span class="built_in">Object</span>.assign(&#123;</span><br><span class="line">      clientLogLevel: <span class="string">&#x27;none&#x27;</span>,</span><br><span class="line">      historyApiFallback: &#123;</span><br><span class="line">        disableDotRule: <span class="literal">true</span>,</span><br><span class="line">        rewrites: [</span><br><span class="line">          &#123; <span class="attr">from</span>: <span class="regexp">/./</span>, to: path.posix.join(options.baseUrl, <span class="string">&#x27;index.html&#x27;</span>) &#125;</span><br><span class="line">        ]</span><br><span class="line">      &#125;,</span><br><span class="line">      contentBase: api.resolve(<span class="string">&#x27;public&#x27;</span>),</span><br><span class="line">      watchContentBase: !isProduction,</span><br><span class="line">      hot: !isProduction,</span><br><span class="line">      quiet: <span class="literal">true</span>,</span><br><span class="line">      compress: isProduction,</span><br><span class="line">      publicPath: options.baseUrl,</span><br><span class="line">      overlay: isProduction <span class="comment">// TODO disable this</span></span><br><span class="line">        ? <span class="literal">false</span></span><br><span class="line">        : &#123; <span class="attr">warnings</span>: <span class="literal">false</span>, <span class="attr">errors</span>: <span class="literal">true</span> &#125;</span><br><span class="line">    &#125;, projectDevServerOptions, &#123;</span><br><span class="line">      https: useHttps,</span><br><span class="line">      proxy: proxySettings,</span><br><span class="line">  &#125;))</span><br><span class="line">  </span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">    compiler.hooks.done.tap(<span class="string">&#x27;vue-cli-service serve&#x27;</span>, <span class="function"><span class="params">stats</span> =&gt;</span> &#123;</span><br><span class="line">    	<span class="comment">// ...</span></span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">    server.listen(port, host, <span class="function"><span class="params">err</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (err) &#123;</span><br><span class="line">          reject(err)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>获取 <code>webpack</code> 配置：<code>api.resolveWebpackConfig()</code></li>
<li>获取 <code>devServer </code>配置</li>
<li>注入 <code>webpack-dev-server</code> 和 <code>hot-reload（HRM）</code>中间件入口</li>
<li>创建 <code>webpack-dev-server</code> 实例</li>
</ul>
<p>启动webpack-dev-server后，在目标文件夹中是看不到编译后的文件的,实时编译后的文件都保存到了内存当中。</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Tab</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://tabxx.github.io/tab.github.io/2019/09/05/vue-cli-service/">https://tabxx.github.io/tab.github.io/2019/09/05/vue-cli-service/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://tabxx.github.io/tab.github.io" target="_blank">Tab</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tab.github.io/tags/CLI/">CLI</a></div><div class="post_share"><div class="social-share" data-image="/tab.github.io/2019/09/05/vue-cli-service/topbg.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-full"><a href="/tab.github.io/2019/09/26/state-pattern/"><img class="prev-cover" src= "" data-lazy-src="/tab.github.io/2019/09/26/state-pattern/rollingLoad.png" onerror="onerror=null;src='/tab.github.io/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">状态模式</div></div></a></div></nav><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src= "" data-lazy-src="/tab.github.io/images/avatar.jpg" onerror="this.onerror=null;this.src='/tab.github.io/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">Tab</div><div class="author-info__description">Tab</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/tab.github.io/archives/"><div class="headline">文章</div><div class="length-num">10</div></a></div><div class="card-info-data-item is-center"><a href="/tab.github.io/tags/"><div class="headline">标签</div><div class="length-num">7</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/Tabxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#vue-cli-service-%E6%9C%BA%E5%88%B6"><span class="toc-number">1.</span> <span class="toc-text">vue-cli-service 机制</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%85%A5%E5%8F%A3"><span class="toc-number">1.1.</span> <span class="toc-text">入口</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%9D%E5%A7%8B%E5%8C%96%E9%85%8D%E7%BD%AE"><span class="toc-number">1.2.</span> <span class="toc-text">初始化配置</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%A7%A3%E6%9E%90%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%8F%82%E6%95%B0"><span class="toc-number">1.3.</span> <span class="toc-text">解析命令行参数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8A%A0%E8%BD%BD%E6%8F%92%E4%BB%B6"><span class="toc-number">1.4.</span> <span class="toc-text">加载插件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E3%80%81%E5%90%AF%E5%8A%A8%E6%9C%8D%E5%8A%A1"><span class="toc-number">1.5.</span> <span class="toc-text">配置、启动服务</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/tab.github.io/2021/08/09/femonitor/" title="前端异常监控平台"><img src= "" data-lazy-src="/tab.github.io/2021/08/09/femonitor/IMG_2992.jpeg" onerror="this.onerror=null;this.src='/tab.github.io/img/404.jpg'" alt="前端异常监控平台"/></a><div class="content"><a class="title" href="/tab.github.io/2021/08/09/femonitor/" title="前端异常监控平台">前端异常监控平台</a><time datetime="2021-08-09T12:25:00.000Z" title="发表于 2021-08-09 20:25:00">2021-08-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/tab.github.io/2021/07/08/ssr/" title="性能优化之SSR初体验"><img src= "" data-lazy-src="/tab.github.io/2021/07/08/ssr/ssr.jpg" onerror="this.onerror=null;this.src='/tab.github.io/img/404.jpg'" alt="性能优化之SSR初体验"/></a><div class="content"><a class="title" href="/tab.github.io/2021/07/08/ssr/" title="性能优化之SSR初体验">性能优化之SSR初体验</a><time datetime="2021-07-08T06:37:22.000Z" title="发表于 2021-07-08 14:37:22">2021-07-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/tab.github.io/2021/05/15/inject/" title="Vue3.0——Provide/Inject的妙用"><img src= "" data-lazy-src="/tab.github.io/2021/05/15/inject/top.jpg" onerror="this.onerror=null;this.src='/tab.github.io/img/404.jpg'" alt="Vue3.0——Provide/Inject的妙用"/></a><div class="content"><a class="title" href="/tab.github.io/2021/05/15/inject/" title="Vue3.0——Provide/Inject的妙用">Vue3.0——Provide/Inject的妙用</a><time datetime="2021-05-15T15:07:43.000Z" title="发表于 2021-05-15 23:07:43">2021-05-15</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2021 By Tab</div><div class="footer_custom_text">Hi, welcome to my <a href="https://tabxx.github.io/tab.github.io/">blog</a>!</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/tab.github.io/js/utils.js"></script><script src="/tab.github.io/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: 'DCF6gBtqrrJYBofXtoomsqy4-gzGzoHsz',
      appKey: 'e9JXPlIRUQ8XLONSE4dsozQA',
      placeholder: 'Please leave your footprints',
      avatar: 'monsterid',
      meta: 'nick,mail,link'.split(','),
      pageSize: '10',
      lang: 'zh-CN',
      recordIP: false,
      serverURLs: '',
      emojiCDN: '',
      emojiMaps: "",
      enableQQ: false,
      path: window.location.pathname,
      requiredFields: ["nick,mail"],
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !false) {
  if (false) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script></div><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>